# import matplotlib.pyplot as plt
import numpy as np
# from utils.data_to_color import ImColor_tp
from scipy.interpolate import griddata

# import glob
# from utils.mssql import sqlDB
# import os
# from multiprocessing import Pool, cpu_count
# import json

latmin, latmax, lonmin, lonmax = 29.25, 30.26, 119.97, 121.22

reso = 0.01
xdim, ydim = int((latmax - latmin) / reso) + 1, int((lonmax - lonmin) / reso) + 1


def datapro(datas, point=False):
    sdata = np.zeros((xdim, ydim))
    r = 1
    for data in datas:
        StationNum, EEEee, NNnn, Precipitation = data
        if StationNum is None or EEEee is None or NNnn is None:
            continue
        x = int((latmax - float(NNnn) / 100) / reso + 0.5)
        y = int((float(EEEee) / 100 - lonmin) / reso + 0.5)
        # latl.append(int((float(NNnn) / 100 - latmin) / reso + 0.5))
        if point:
            Precipitation = 100
        if not Precipitation:
            Precipitation = 0
        sdata[x - r:x + r, y - r:y + r] = float(Precipitation) / 10
    return sdata


def interp(datas):
    lonl, latl, values = [], [], []
    for data in datas:
        StationNum, EEEee, NNnn, Precipitation = data
        if StationNum is None or EEEee is None or NNnn is None:
            continue
        lonl.append(int((float(EEEee) / 100 - lonmin) / reso + 0.5))
        latl.append(int((latmax - float(NNnn) / 100) / reso + 0.5))
        # latl.append(int((float(NNnn) / 100 - latmin) / reso + 0.5))
        if not Precipitation:
            Precipitation = 0
        values.append(float(Precipitation) / 10)

    grid_lat = np.arange(xdim)
    grid_lon = np.arange(ydim)
    lon_grid, lat_grid = np.meshgrid(grid_lon, grid_lat)

    _points = (lonl, latl)

    # nearest = griddata(_points, values, (lon_grid, lat_grid), method='nearest', rescale=True)
    # linear = griddata(_points, values, (lon_grid, lat_grid), method='linear', rescale=True)
    if len(values) * np.max(values) == 0:
        cubic = np.zeros((xdim, ydim))
    else:
        cubic = griddata(_points, values, (lon_grid, lat_grid), method='cubic', fill_value=0, rescale=True)

    pxs, pys = [], []
    st = 3
    for x in range(xdim):
        for y in range(ydim):
            x0 = max(0, x - st)
            x1 = min(xdim, x + st)
            y0 = max(0, y - st)
            y1 = min(ydim, y + st)

            era = cubic[x0:x1, y0:y1] > 0

            if era.sum() < 1:
                pxs.append(x)
                pys.append(y)
    cubic[([pxs], [pys])] = 0
    cubic[cubic < 0] = 0
    cubic = np.nan_to_num(cubic)

    return cubic

    # sdir = f"{label_path}/{fname[:8]}"
    # if not os.path.exists(sdir):
    #     os.mkdir(sdir)
    # np.savetxt(f"{sdir}/{fname[:12]}.000", cubic, fmt='%.02f')


if __name__ == '__main__':
    s_time = '202001051220'
    # test_DB = sqlDB()
    # # 用一个rs变量获取数据
    # result = test_DB.select(s_time)
    # print(result)
    # interp(result)
    # with open('/home/gym/projects/my_test/data/idxs.json') as f:
    #     idxs = tuple(np.array(json.load(f)).T)
    # files = glob.glob(f"{station_grid_path}/20211*.npy")
    # for f in files:
    #     interp(f)
    #     exit()

    # pool = Pool(processes=20)
    # pool.map(interp, files)
    # pool.close()
    # pool.join()
